package com.artron.mediaartron.ui.widget;

import android.content.Context;
import android.content.res.Configuration;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.artron.mediaartron.R;
import com.artron.mediaartron.ui.helper.BoxingResHelper;
import com.bilibili.boxing.BoxingMediaLoader;
import com.bilibili.boxing.model.entity.BaseMedia;
import com.bilibili.boxing.model.entity.impl.ImageMedia;

/**
 * Class description here
 *
 * @author doing
 * @version 1.0.0
 * @since 2017-09-27.
 */

public class MediaImageLayout extends FrameLayout {

    private static final int BIG_IMG_SIZE = 5 * 1024 * 1024;

    private ImageView mCheckImg;
    private View mFontLayout;
    private ImageView mCoverImg;
    private MediaImageLayout.ScreenType mScreenType;

    private enum ScreenType {
        SMALL(100), NORMAL(180), LARGE(320);
        int value;

        ScreenType(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }
    }

    public MediaImageLayout(Context context) {
        this(context, null, 0);
    }

    public MediaImageLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MediaImageLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View view = LayoutInflater.from(context).inflate(R.layout.item_picture_selection_image_content, this, true);
        mCoverImg = (ImageView) view.findViewById(R.id.PictureSelectionFragmentItem_iv_cover);
        mCheckImg = (ImageView) view.findViewById(R.id.PictureSelectionFragmentItem_iv_check);
        mFontLayout = view.findViewById(R.id.PictureSelectionFragmentItem_foreground);
        mScreenType = getScreenType(context);
        setImageRect(context);
    }

    private void setImageRect(Context context) {
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        int screenHeight = metrics.heightPixels;
        int screenWidth = metrics.widthPixels;
        int width = 100;
        if (screenHeight != 0 && screenWidth != 0) {
            width = (screenWidth - getResources().getDimensionPixelOffset(R.dimen.boxing_media_margin) * 4) / 3;
        }
        mCoverImg.getLayoutParams().width = width;
        mCoverImg.getLayoutParams().height = width;
        mFontLayout.getLayoutParams().width = width;
        mFontLayout.getLayoutParams().height = width;
    }

    private MediaImageLayout.ScreenType getScreenType(Context context) {
        int type = context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
        MediaImageLayout.ScreenType result;
        switch (type) {
            case Configuration.SCREENLAYOUT_SIZE_SMALL:
                result = MediaImageLayout.ScreenType.SMALL;
                break;
            case Configuration.SCREENLAYOUT_SIZE_NORMAL:
                result = MediaImageLayout.ScreenType.NORMAL;
                break;
            case Configuration.SCREENLAYOUT_SIZE_LARGE:
                result = MediaImageLayout.ScreenType.LARGE;
                break;
            default:
                result = MediaImageLayout.ScreenType.NORMAL;
                break;
        }
        return result;
    }

    public void setImageRes(@DrawableRes int imageRes) {
        if (mCoverImg != null) {
            mCoverImg.setImageResource(imageRes);
        }
    }

    public void setMedia(BaseMedia media) {
        setCover(((ImageMedia) media).getThumbnailPath());
    }

    private void setCover(@NonNull String path) {
        if (mCoverImg == null || TextUtils.isEmpty(path)) {
            return;
        }
        mCoverImg.setTag(R.string.app_name , path);
        BoxingMediaLoader.getInstance().displayThumbnail(mCoverImg, path, mScreenType.getValue(), mScreenType.getValue());
    }

    @SuppressWarnings("deprecation")
    public void setChecked(boolean isChecked) {
        if (isChecked) {
            mFontLayout.setVisibility(View.VISIBLE);
            mCheckImg.setImageDrawable(getResources().getDrawable(BoxingResHelper.getMediaCheckedRes()));
        } else {
            mFontLayout.setVisibility(View.GONE);
            mCheckImg.setImageDrawable(getResources().getDrawable(BoxingResHelper.getMediaUncheckedRes()));
        }
    }
}
